package relu.R.landscape;
import jfm.model.Types.*;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import jfm.model.Types;
import jfm.xml.XMLSyntaxException;

public class EnviroVsProfitDistribution extends SocioEconomicDistribution {
	private Map<String,HashMap<CropType,Double>> cropPrices=new HashMap<String,HashMap<CropType,Double>>();
	private String priceScenario=null;

	
	public void setCropPricesForScenario(String scen){
		if ( cropPrices.containsKey(scen)){
			priceScenario=scen;
		} else {
			throw new Error("No price scenario named "+scen);
		}
	}
	
	
	public void setCropPriceScenarios(String csvFile){
		ArrayList<String> lines = readLines(csvFile);
		String[] header=lines.get(0).split(",");
		ArrayList<CropType> crops=new ArrayList<CropType>();
		try {
		for (int i=1;i<header.length;i++){
			crops.add(Types.xmlToCropType(header[i]));
		}} catch (XMLSyntaxException ex){
			throw new Error(ex.getMessage());
		}
		
		for(int i=1;i<lines.size();i++){
			String[] vals=lines.get(i).split(",");
			String scenario=vals[0];
			cropPrices.put(scenario, new HashMap<CropType,Double>());
			for(int j=1;j<vals.length;j++){
				cropPrices.get(scenario).put(crops.get(j-1),Double.parseDouble(vals[j]));
			}
		}
	}
	
	/** Pick a set of values from the distribution and use them to set 
	 * the characteristics of this individual farm */
	public void sampleFarmer(LSFarm farm) {
		// Apply the crop Prices to this farm 
		if ( priceScenario==null){
			throw new Error("Price scenario not yet set");
		}
		Map<CropType,Double> prices=cropPrices.get(priceScenario);
		for(CropType ct:prices.keySet()){
			farm.setPriceForCrop(ct,prices.get(ct));
		}
		
		// Choose an MOU to apply to this farmer

	}
	

	

	private static ArrayList<String> readLines(String fileName){
		ArrayList<String> lines = new ArrayList<String>();
		final BufferedReader reader;
		try {
			reader=new BufferedReader(new FileReader(fileName));
			String nextLine=reader.readLine();
			int lineCount=1;
			while(nextLine!=null){
				lines.add(nextLine);
				nextLine=reader.readLine();
			}
			
		} catch (FileNotFoundException ex){
			throw new Error(ex.getMessage());
		} catch (IOException ex){
			throw new Error(ex.getMessage());
		}
		return lines;
	}
	
}
